package cn.itcast.jx.action.sysadmin;

import java.io.IOException;
import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import java.util.Set;

import javax.servlet.http.HttpServletResponse;

import org.apache.struts2.ServletActionContext;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.opensymphony.xwork2.ModelDriven;

import cn.itcast.jx.action.BaseAction;
import cn.itcast.jx.domain.Dept;
import cn.itcast.jx.domain.Role;
import cn.itcast.jx.domain.User;
import cn.itcast.jx.service.IDeptService;
import cn.itcast.jx.service.RoleService;
import cn.itcast.jx.service.UserService;
import cn.itcast.jx.util.Encrypt;
import cn.itcast.jx.util.JSONUtils;
import cn.itcast.jx.util.Page;

public class UserAction extends BaseAction implements ModelDriven<User>{

	private IDeptService deptService;
	
	private String deptId;
	
	private String menuType = "user";
	
	private RoleService roleService;
	
	private List<User> userManager = new ArrayList<>();
	
	private List<String> roleIds;
	

	private UserService userService;
	private User model = new User();
	
	private Page page = new Page();
	
	
	public String list() {
		String hql = "from User";
		userService.findPage(hql, page, User.class, null);
		page.setUrl("userAction_list");
		push(page);
		return "list";
	}
	
	public String toview() {
		User user = userService.get(User.class, model.getId());
		push(user);
		return "toview";
	}
	
	
	public String tocreate() {
		String hql = "from Dept d where d.state=1";
		List<Dept> deptList = deptService.find(hql, Dept.class, null);
		String hql1 = "from User where state=1 ";
		List<User> userList = userService.find(hql1, User.class, null);
		
		put("deptList", deptList);
		put("userList", userList);
		
		return "tocreate";
	}
	
	public String insert() throws Exception {
		userService.saveOrUpdate(model);
		return "tolist";
	}
	public String toupdate() {
		String hql = "from Dept d where d.state=1";
		List<Dept> deptList = deptService.find(hql, Dept.class, null);
		
		User user = userService.get(User.class, model.getId());
		
		put("deptList", deptList);
		push(user);
		
		return "toupdate";
	}
	public String update() throws Exception {
		
		userService.saveOrUpdate(model);
		
		return "tolist";
	}
	
	public String delete() {
		String[] ids = model.getId().split(", ");
		
		userService.delete(User.class, ids);
		return "tolist";
	}
	
	public String torole() {
		List<Role> roleList = roleService.find("from Role", Role.class, null);
		
		User user = userService.get(User.class, model.getId());
		Set<Role> roles = user.getRoles();
		
		String userRoleStr = "";
		
		for (Role role : roles) {
			userRoleStr += role.getName() + ",";
		}
		
		put("roleList", roleList);
		push(user);
		put("userRoleStr", userRoleStr);
		return "torole";
	}

	public String showManager() {
		String hql1 = "from User where state=1 and dept.id=?";
		List<User> list = userService.find(hql1, User.class, new Object[] {model.getId()});
		if(list != null && list.size() != 0) {
			String str = "";
			str +="[";
			for (User user : list) {
				str +="{id:'"+user.getId()+"',name:'"+user.getUserInfo().getName()+"'},";
			}
			str = str.substring(0, str.length() - 1);
			str += "]";
			JSONUtils.writeJson(str);
		}
		
		return NONE;
	}
	
	/*public String changeManager() throws Exception {
		String hql = "from User where dept.id = ? and userInfo.degree!=4";
		List<User> userList = userService.find(hql, User.class, new String[]{deptId});
		push(userList);
		return "json";
	}*/
	public String changeManager() throws Exception {
		String hql = "from User where dept.id = ? and userInfo.degree!=4";
		List<User> userList = userService.find(hql, User.class, new String[]{deptId});
		
		String str = JSON.toJSONString(userList);
		
		JSONUtils.writeJson(str);
		return NONE;
	}
	
	public String role() {
		User user = userService.get(User.class, model.getId());
		Set<Role> roleList = new HashSet<>();
		for (String id : roleIds) {
			roleList.add(roleService.get(Role.class, id));
		} 
		user.setRoles(roleList);
		userService.updateRole(user);
		return "tolist";
	}
	
	
	
	@Override
	public User getModel() {
		// TODO Auto-generated method stub
		return model;
	}
	public Page getPage() {
		return page;
	}

	public void setPage(Page page) {
		this.page = page;
	}

	public void setUserService(UserService userService) {
		this.userService = userService;
	}
	public String getDeptId() {
		return deptId;
	}
	public void setDeptId(String deptId) {
		this.deptId = deptId;
	}
	
	public List<User> getUserManager() {
		return userManager;
	}

	public void setUserManager(List<User> userManager) {
		this.userManager = userManager;
	}

	public List<String> getRoleIds() {
		return roleIds;
	}
	
	public void setRoleIds(List<String> roleIds) {
		this.roleIds = roleIds;
	}
	
	
	public void setRoleService(RoleService roleService) {
		this.roleService = roleService;
	}
	
	public void setDeptService(IDeptService deptService) {
		this.deptService = deptService;
	}

	public String getMenuType() {
		return menuType;
	}

	public void setMenuType(String menuType) {
		this.menuType = menuType;
	}
}
